<template>
  <div class="draw-history-container">
    <div class="search-bar">
      <el-select v-model="prize_type" placeholder="请选择类型">
        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>
      <el-input v-model="searchMobile" placeholder="请输入手机号搜索" prefix-icon="el-icon-search" style="width: 300px;" />
      <el-button type="primary" @click="handleSearch" style="margin-left: 10px;">
        搜索
      </el-button>
      <el-button @click="resetSearch" style="margin-left: 10px;">
        重置
      </el-button>
    </div>

    <el-table v-loading="loading" :data="drawHistoryList" style="width: 100%" border>
      <el-table-column prop="id" label="ID" width="80" />
      <!-- <el-table-column prop="user_id" label="用户ID" width="100" /> -->
      <el-table-column label="用户信息" width="auto">
        <template slot-scope="scope">
          <div>{{ scope.row.user.nickname }}</div>
          <div>{{ scope.row.user.remark }}</div>
          <div>{{ scope.row.user.mobile }}</div>
        </template>
      </el-table-column>
      <el-table-column label="奖品信息" width="200">
        <template slot-scope="scope">
          <div class="prize-info">
            <img :src="scope.row.icon" alt="奖品图标" class="prize-icon" />
            <div class="prize-details">
              <div>{{ scope.row.prize_name }}</div>
              <!-- <div>价值: {{ scope.row.prize_value }}</div> -->
            </div>
          </div>
        </template>
      </el-table-column>

      <el-table-column prop="address" label="收货地址" width="400">
        <template slot-scope="scope" v-if="scope.row.address">
          <p>
            {{ scope.row.address.province }}
            {{ scope.row.address.city }}
            {{ scope.row.address.district }}
            {{ scope.row.address.detail }}
          </p>
          <p>
            收货人： {{ scope.row.address.
              consignee }}
            手机号： {{ scope.row.mobile }}
          </p>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="收货状态" width="400">
        <template slot-scope="scope" v-if="scope.row.address">
          <p>
            
            {{ scope.row.status == 2 ? '已到货' : '发货中'  }}
          </p>
        </template>
      </el-table-column>
      <el-table-column prop="create_time" label="创建时间" width="180" />
      <el-table-column label="操作(是否到货)" align="center">
        <template slot-scope="scope" v-if="scope.row.address">
          <el-switch v-model="scope.row.status" @change="onitemChange(scope.row)" :active-value="2">
          </el-switch>
        </template>


      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination layout="total, sizes, prev, pager, next, jumper" :total="total" :page-size.sync="pageSize"
        :current-page.sync="currentPage" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
  </div>
</template>

<script>
import { status } from 'nprogress';
import { getDrawHistory , setDrawStatus } from '../../../libs/api'; // 假设API接口在这个路径

export default {
  name: 'DrawHistory',
  data() {
    return {
      drawHistoryList: [],
      total: 0,
      pageSize: 15,
      currentPage: 1,
      loading: false,
      searchMobile: '',
      prize_type: '',

      options: [{
        value: 'coupon',
        label: '优惠券'
      }, {
        value: 'points',
        label: '球钻'
      }, {
        value: 'physical',
        label: '实物'
      }],
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.loading = true;
      const params = {
        page: this.currentPage,
        per_page: this.pageSize
      };

      // 如果有手机号搜索条件，则添加到参数中
      params.mobile = this.searchMobile;
      params.prize_type = this.prize_type;
      getDrawHistory(params)
        .then(res => {
          console.log(res)
          this.drawHistoryList = res.data.data;
          this.total = res.data.total;
          this.loading = false;
        })
        .catch(err => {
          console.error('获取抽奖记录失败:', err);
          this.loading = false;
        });
    },

    handleSearch() {
      this.currentPage = 1; // 搜索时重置到第一页
      this.fetchData();
    },
    onitemChange(v) {
      setDrawStatus({
        id: v.id,
        status: v.status
      })
      
    },
    resetSearch() {
      this.searchMobile = '';
      this.prize_type = ''
      this.currentPage = 1;
      this.fetchData();
    },

    handleSizeChange(size) {
      this.pageSize = size;
      this.fetchData();
    },

    handleCurrentChange(current) {
      this.currentPage = current;
      this.fetchData();
    }
  }
};
</script>

<style scoped>
.draw-history-container {
  padding: 20px;
  background-color: #fff;
}

.search-bar {
  margin-bottom: 20px;
  display: flex;
  align-items: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.prize-info {
  display: flex;
  align-items: center;
}

.prize-icon {
  width: 40px;
  height: 40px;
  border-radius: 4px;
  margin-right: 10px;
}

.prize-details {
  display: flex;
  flex-direction: column;
}
</style>